// pages/clothKids/clothKids.js
var util = require("../../utils/util");
var clothUtil = require("../../utils/clothUtil.js");

Page({
  data:{
    productList : [], // 商品列表
    isNew : true, // 最新或者最热
    hideMask : true, // 是否隐藏遮罩层
    hideSearchMask: true, // 是否隐藏搜索遮罩
    loadHide: true,
    filterCatHide: true,
    filterFiltHide: true,
    configCatList: getApp().globalData.KIDS_CATEGORY,
    configFiltList: getApp().globalData.ALL_FILTER,
    selectedCatId : 0,  // 已经选择了分类id
    selectedCatName: '全部', // 已选择分类名称
    selectedFilt : {}, // 已经选择了的id数组
    filterBtnOn : true,
    pageNo : 0,  // 当前分页
    pageSize : 20, // 单页数量
    totalPage : 0, // 总页数
    totalNum : 0 // 总数量
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    if(getApp().globalData.KIDS_CATEGORY.length == 0){
      clothUtil.getChildCategory('KIDS_CATEGORY_ID', 'KIDS_CATEGORY', function(data){
        console.log(data);
        that.setData({configCatList: data});
      }, true);
    }else{
      this.setData({configCatList: getApp().globalData.KIDS_CATEGORY});
    }
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight:res.windowHeight
        });
      }
    });
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight:res.windowHeight
        });
      }
    });
    clothUtil.getClothList(this, 0, 'KIDS_CATEGORY_ID');
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  bindDownload: function(){
    // 该方法绑定页面滑动到底部事件
    console.log("download");
    this.setData({
      loadHide:false
    });
    clothUtil.getClothList(this, 0, 'KIDS_CATEGORY_ID');
  },
  scroll: function(e){
    // 滚动事件
    // this.setData({
    //   scrollTop: e.detail.scrollTop
    // });
  },
  onShowFilterCat: function(){
    this.setData({
      hideMask:false,
      filterCatHide:false
    });
  },
  onShowFilterFilt: function(){
    this.setData({
      hideMask:false,
      filterFiltHide:false
    });
  },
  onFilterFiltTxt: function(e){
    console.log(this.data.selectedFilt);
    var trueNum = 0;
    var name = e.currentTarget.dataset.filtname; // field_name
    var newObj = {};
    for(var i in this.data.selectedFilt){
      if(i != name && this.data.selectedFilt[i]) trueNum++;
      newObj[i] = this.data.selectedFilt[i];
    }
    if(!this.data.selectedFilt[name]) trueNum++;
    newObj[name] = !this.data.selectedFilt[name];
    
    console.log(trueNum);
    
    this.setData({
       selectedFilt: newObj,
       filterBtnOn: trueNum
    });
  },
  clearFilter: function(){
    this.setData({
      filterBtnOn: false,
      selectedFilt: {}
    });
  },
  
  onHideMask:function(){
    this.setData({
      hideMask:true,
      filterCatHide:true,
      filterFiltHide:true
    });
  },
  changeBigCat: function(e){
    var id = e.currentTarget.dataset.catid;
    this.setData({catNo: id});
    clothUtil.getClothList(this, 1, 'KIDS_CATEGORY_ID');
  },
  changeCat: function(e){
    var cid = e.currentTarget.dataset.catid;
    var catname = e.currentTarget.dataset.catname;
    this.setData({
      selectedCatId: cid,
      selectedCatName: catname,
      hideMask:true,
      loadHide:false
    });
    clothUtil.getClothList(this, 1, 'KIDS_CATEGORY_ID');
  },
  changeFilter: function(){
    // if(this.data.filterBtnOn){
      this.setData({
        hideMask:true,
        loadHide:false
      });
      clothUtil.getClothList(this, 1, 'WOMEN_CATEGORY_ID');
    // }
  },
  changeNew : function(e){
      var isnew = e.currentTarget.dataset.isnew;
      this.setData({
        isNew: isnew == 1 ? true : false
      });
      clothUtil.getClothList(this, 1, 'KIDS_CATEGORY_ID');
  },

  gotoClothDetail: function(e){
      var productId = e.currentTarget.dataset.productid;
      var productName = e.currentTarget.dataset.productname;
      wx.navigateTo({
        url: '../clothDetail/clothDetail?productId='+productId+'&productName='+productName
      })
  },

  searchCloth: function(e){
    // 搜索
    console.log(e.detail.value);
    var keyword = util.trim(e.detail.value);
    if(keyword == "") return;
    wx.navigateTo({
        url: '../clothSearch/clothSearch?searchType=1&keyword='+keyword
    })
  },
  searchClothTxt: function(){},
  onFocusSearch: function(){
    this.setData({hideSearchMask: false});
  },
  onBlurSearch: function(){this.setData({hideSearchMask: true});},
  onScanCode: function(){
    wx.scanCode({
      success: function(res){
        var keyword = util.trim(res.result);
        if(keyword == "") return;
        wx.navigateTo({
            url: '../clothSearch/clothSearch?searchType=3&keyword='+keyword
        })
      },
      fail: function() {
        // fail
        wx.showModal({
          title: '提示',
          content: '识别失败，请确认后重新扫描',
          showCancel: false,
          confirmText: '确认',
          confirmColor: '#111111',
          success: function(res){
          },
          fail: function(){}
        });
      },
      complete: function() {
        // complete
      }
    })
  },
  onShareAppMessage: function () {
    return {
      title: '北江纺织丹宁成衣平台',
      path: '/pages/clothKids/clothKids'
    }
  }
})